<script>
import axios from "axios";
import {ref} from "vue";

export default {
  setup() {
    //修改 弹窗
    const updateVisible = ref(false);
    //新增弹窗
    const saveVisible = ref(false);
    return {
      updateVisible,
      saveVisible,
    };
  },
  name: "Manage",
  data() {
    return {
      car:{
        id:'',
        vehicleNumber:'',
        vehicleType:'',
        capacity:''
      },
      // 分页
      pageDto:{
        pageNum:1,
        pageSize:3,
        data:{
        }
      },
      // 总页数
      total:0,
      // 总页码
      pages:0,
      carList:[]
    }
  },
  methods:{
    getAllCar(){
      axios.post("/oa/cars/getAllCar",this.pageDto).then((res)=>{
        this.pageDto.pageNum=res.data.data.pageNum;
        this.pageDto.pageSize = res.data.data.pageSize;
        this.total=res.data.data.total;
        this.pages=res.data.data.pages;
        this.carList=res.data.data.list;
      })
    },
    deleteCar(carId){
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
      axios.delete(`/oa/cars/deleteCar/${carId}`).then((res)=>{
        if(res.data.code === 1){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getAllCar();
        }else {
          this.$message({
            type: 'error',
            message: '删除成功!'
          });
        }
      })
    }).catch(
          () => {
            this.$message({
              type: 'info',
              message: '已取消'
            });
          }
      );
    },
    getCar(carId){
      this.updateVisible = true;
      // 根据id 查询数据
      axios.get(`/oa/cars/getCarById/${carId}`).then((res)=>{
        this.car = res.data.data;
      })
    },
    updateCar(){
      axios.put("/oa/cars/updateCar",this.car).then((res)=>{
        if(res.data.code === 1){
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.updateVisible = false;
          this.getAllCar();
        }else {
          this.$message({
            type: 'error',
            message: '修改失败!'
          });
        }
      })
    },
    saveCar(){
      if(this.car.vehicleNumber === null || this.car.vehicleNumber === undefined || this.car.vehicleNumber === ""){
        this.$message({
          type: 'warning',
          message: '车牌号不能为空!'
        });
        return;
      }
      if(this.car.vehicleType === null || this.car.vehicleType === undefined || this.car.vehicleType === ""){
        this.$message({
          type: 'warning',
          message: '车型不能为空!'
        });
        return;
      }
      if(this.car.capacity === null || this.car.capacity === undefined || this.car.capacity === ""){
        this.$message({
          type: 'warning',
          message: '载客人数不能为空!'
        });
        return;
      }
      axios.post("/oa/cars/saveCar",this.car).then((res)=>{
        if(res.data.code === 1){
          this.$message({
            type: 'success',
            message: '添加成功!'
          });
          this.saveVisible = false;
          this.getAllCar();
        }else {
          this.$message({
            type: 'error',
            message: '添加失败!'
          });
        }
      })
    },saveButton(){
      this.saveVisible = true
      this.car={}
    }
  },
  created() {
    this.getAllCar();
  }
}
</script>

<template>
  <!--标头显示-->
  <div  class="titleBox">
    车辆信息管理
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <el-button type="success" size="small" @click="saveButton()">新增</el-button>
  </div>
  <!-- 首页表格信息显示 -->
  <el-table :data="this.carList" style="width: 100%">
    <el-table-column label="车牌号">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.vehicleNumber}}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="车型">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.vehicleType }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="载客人数">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <span style="margin-left: 10px">{{ scope.row.capacity }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="danger" size="small" @click="deleteCar(scope.row.id)">删除</el-button>
        <el-button type="primary" size="small" @click="getCar(scope.row.id)">修改</el-button>
      </template>
    </el-table-column>
  </el-table>


  <!-- 分页-->
  <el-pagination
      v-model:current-page="this.pageDto.pageNum"
      :page-size="this.pageDto.pageSize"
      :pager-count="this.pages"
      layout="prev, pager, next"
      :total="this.total"
      @current-change="getAllCar"
  />
  <!--// 修改弹窗-->
  <el-dialog
      v-model="updateVisible"
      title="修改车辆信息"
      width="600"
      :before-close="handleClose"
  >
    <el-form-item label="车牌号:" prop="userCord" >
      <el-input
          v-model="car.vehicleNumber"
          type="text"
          autocomplete="off"
          aria-required="true"
      />
    </el-form-item>
    <el-form-item label="车型:" prop="userCord" >
      <el-input
          v-model="car.vehicleType"
          type="text"
          autocomplete="off"
          aria-required="true"
      />
    </el-form-item>
    <el-form-item label="载客人数:" prop="userCord" >
      <el-input
          v-model="car.capacity"
          type="text"
          autocomplete="off"
          aria-required="true"
      />
    </el-form-item>
    <el-button type="primary" @click="updateCar()">确定</el-button>
    <el-button @click="updateVisible = false">取消</el-button>
  </el-dialog>

  <el-dialog
      v-model="saveVisible"
      title="新增车辆信息"
      width="600"
      :before-close="handleClose"
  >
    <el-form-item label="车牌号:" prop="userCord" >
      <el-input
          v-model="car.vehicleNumber"
          type="text"
          autocomplete="off"
          aria-required="true"
          placeholder="请输入车牌号"
      />
    </el-form-item>
    <el-form-item label="车型:" prop="userCord" >
      <el-input
          v-model="car.vehicleType"
          type="text"
          autocomplete="off"
          aria-required="true"
          placeholder="请输入车型"
      />
    </el-form-item>
    <el-form-item label="载客人数:" prop="userCord" >
      <el-input
          v-model="car.capacity"
          type="text"
          autocomplete="off"
          aria-required="true"
          placeholder="请输入载客人数"
      />
    </el-form-item>

    <el-button type="primary" @click="saveCar()">确定</el-button>
    <el-button @click="saveVisible = false">取消</el-button>
  </el-dialog>
</template>


<style scoped lang="less">

</style>
